package org.zzul.ctrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zzul.util.TokenUtil;


/*
 * 请求--zuul--users服务验证
 */
@RestController
public class ZzulController {
//    @Autowired
	
//    private StringRedisTemplate strTemplate;
    
    @Autowired
    private RedisTemplate redisTemplate; 
    @Autowired
    private AdminFeign adminFeign;

    @RequestMapping("/selectLogin")
    public String selectLogin(String aid, String apwd){
    	System.out.println("zuul--");
        //用feign访问管理员服务，验证账号密码
        String result = adminFeign.checkLogin(aid, apwd);
        System.out.println("result:"+result);
        try {
            if(result != null){
                //如果验证通过，生成token，保存redis
                String token = TokenUtil.sign(aid);
                redisTemplate.opsForValue().set(token, aid);
//                strTemplate.opsForValue().set(token, aid, 60, TimeUnit.MINUTES);
                return token;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }
    
	@RequestMapping("/test2")
	public void test() {
		System.out.println("1112");
//		redisTemplate.opsForValue().set("msg7", "abc");
		String str=(String)redisTemplate.opsForValue().get("msg7");
		System.out.println(str);
	}
	@Autowired(required = false)
	public void setRedisTemplate(RedisTemplate redisTemplate) {
	    RedisSerializer stringSerializer = new StringRedisSerializer();
	    redisTemplate.setKeySerializer(stringSerializer);
	    redisTemplate.setValueSerializer(stringSerializer);
	    redisTemplate.setHashKeySerializer(stringSerializer);
	    redisTemplate.setHashValueSerializer(stringSerializer);
	    this.redisTemplate = redisTemplate;
	}
}
